package com.losdun.product.service.impl;

import com.losdun.common.utils.DateUtils;
import com.losdun.product.domain.Config;
import com.losdun.product.domain.Parts;
import com.losdun.product.domain.vo.PartsOptionsVo;
import com.losdun.product.mapper.PartsMapper;
import com.losdun.product.service.IConfigService;
import com.losdun.product.service.IPartsService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产品配件Service业务层处理
 * 
 * @author sword.ye
 * @date 2025-03-02
 */
@Service
public class PartsServiceImpl implements IPartsService 
{
    @Autowired
    private PartsMapper partsMapper;
    @Autowired
    private IConfigService configService;

    /**
     * 查询产品配件
     * 
     * @param id 产品配件主键
     * @return 产品配件
     */
    @Override
    public Parts selectPartsById(Integer id)
    {
        return partsMapper.selectPartsById(id);
    }

    /**
     * 查询产品配件列表
     * 
     * @param parts 产品配件
     * @return 产品配件
     */
    @Override
    public List<Parts> selectPartsList(Parts parts)
    {
        return partsMapper.selectPartsList(parts);
    }

    /**
     * 新增产品配件
     * 
     * @param parts 产品配件
     * @return 结果
     */
    @Override
    public int insertParts(Parts parts)
    {
        parts.setCreateTime(DateUtils.getNowDate());
        return partsMapper.insertParts(parts);
    }

    /**
     * 修改产品配件
     * 
     * @param parts 产品配件
     * @return 结果
     */
    @Override
    public int updateParts(Parts parts)
    {
        parts.setUpdateTime(DateUtils.getNowDate());
        return partsMapper.updateParts(parts);
    }

    /**
     * 批量删除产品配件
     * 
     * @param ids 需要删除的产品配件主键
     * @return 结果
     */
    @Override
    public int deletePartsByIds(Integer[] ids)
    {
        return partsMapper.deletePartsByIds(ids);
    }

    /**
     * 删除产品配件信息
     * 
     * @param id 产品配件主键
     * @return 结果
     */
    @Override
    public int deletePartsById(Integer id)
    {
        return partsMapper.deletePartsById(id);
    }

    /**
     * 返回选择树
     * @return
     */
    @Override
    public List<PartsOptionsVo> selectPartsOptionsVoList() {
       List<Parts> partsList=partsMapper.selectPartsList(new Parts());
       if(CollectionUtils.isNotEmpty(partsList)){
           Map<Long, List<Parts>> groupCollect = partsList.stream().collect(Collectors.groupingBy(Parts::getConfigId));
           List<PartsOptionsVo> voList=new ArrayList<>();
           for(Long id : groupCollect.keySet()){
              Config config=configService.selectConfigById(id);
              PartsOptionsVo partsOptionsVo=new PartsOptionsVo();
              partsOptionsVo.setId(config.getId());
              partsOptionsVo.setLabel(config.getName());
               List<Parts> plist=groupCollect.get(id);
              List<PartsOptionsVo> childrenList=plist.stream().map(t->{
                   PartsOptionsVo children=new PartsOptionsVo();
                   children.setId(Long.valueOf(t.getId()));
                   children.setLabel(t.getPartName());
                   children.setChildren(Collections.emptyList());
                   return children;
               }).collect(Collectors.toList());
              partsOptionsVo.setChildren(childrenList);
              voList.add(partsOptionsVo);
           }
           return voList;
       }
        return Collections.emptyList();
    }
}
